﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Collections;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Data.SqlClient;

namespace DISPLAY_CLIENT
{
    class Listener
    {
        public string ServerIP = ConfigurationManager.AppSettings["采集服务IP"];
        private Socket SockServer = null;
        private Thread listenThread;
        private bool isStop = true;

        public delegate void OndataReceive(byte[] databyte);
        public event OndataReceive onReceive;

        public bool Register()
        {
            if (ServerIP.Length == 0)
            {
                IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress[] ipList = IpEntry.AddressList;

                foreach (IPAddress ip in ipList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ServerIP = ip.ToString();
                        break;
                    }
                }
            }
            
            if (ServerIP.Length > 0)
            {
                if (ServerIP == "-")
                {
                    return true;
                }

                try
                {
                    SockServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    SockServer.Connect(IPAddress.Parse(ServerIP), 9001);
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            return false;
        }

        public void StartListen()
        {
            isStop = false;

            listenThread = new Thread(new ThreadStart(ListenThread));
            listenThread.IsBackground = true;
            listenThread.Start();
        }

        public void StopListen()
        {
            if (isStop)
            {
                return;
            }

            isStop = true;

            SockServer.Close();
            listenThread.Abort();
        }

        public void ListenThread()
        {
            Byte[] bReceive = new Byte[1024 * 1024 * 10];
            SockServer.ReceiveBufferSize = bReceive.Length;
            int receiveLength = 0;
            while (!isStop)
            {
                try
                {
                    receiveLength = SockServer.Receive(bReceive, bReceive.Length, SocketFlags.None);
                    byte[] databyte = new byte[receiveLength];

                    Array.Copy(bReceive, databyte, receiveLength);
                    if (onReceive != null)
                    {
                        onReceive(databyte);
                    }
                }
                catch { }
            }
        }
    }
}
